package com.lwz.laboratory.controller;


import com.lwz.laboratory.entity.*;
import com.lwz.laboratory.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Collection;
import java.util.List;

@Controller
public class SignupController {
    @Autowired
    PostService postService;

    @Autowired
    RecruitplanService recruitplanService;

    @Autowired
    SignupService signupService;

    @Autowired
    ArchivsesService archivsesService;

    @Autowired
    UserService userService;
    @Autowired
    InterviewService interviewService;

    @Autowired
    RegisterService registerService;

    @Autowired
    LaboratoryService laboratoryService;






//查看全部报名信息
    @RequestMapping("/allsignup/")
    public String AllSignup( String applicant, Model model){
        List<Signup> signups =signupService.findallsignforme(applicant);



        model.addAttribute("signups",signups);

        return "plan/plan_allsign";

    }
    //查看通过审核的报名信息
    @RequestMapping("/passsignup/")
    public String PassSignup(String applicant, Model model){
        List<Signup> signups =signupService.findpasssign(applicant);



        model.addAttribute("signups",signups);

        return "signup/signup_signpass";

    }










//查看我的报名信息
    @RequestMapping("/mysignup/{stuid}")
    public String MySignup(@PathVariable String stuid, Model model){
        List<Signup> signup =signupService.findsignforme(stuid);



        model.addAttribute("signups",signup);

        return "signup/my_sign";

    }
    //审核报名信息
    @RequestMapping("/checksignup/{stuid}")
    public String tochecksignup(@PathVariable String stuid, Model model){
        List<Signup> signup =signupService.findsignforme(stuid);

        model.addAttribute("signups",signup);

        return "signup/check_signup";

    }

    @RequestMapping("/checksignup")
    public String checksignup(Signup signup, Model model,String applicant1){

        signupService.EditSignup(signup);
        System.out.println(signup.getSignid());
        System.out.println(signup.getStatus());

        List<Signup> signups =signupService.findallsignforme(applicant1);

        model.addAttribute("signups",signups);

        return AllSignup(applicant1,model);


    }





    //报名指定岗位
    @GetMapping("/signup")
    public String toSign(String postid, Model model){
        Post post =postService.findonebypostid(postid);
        model.addAttribute("posts",post);

        return "plan/plan_signup";

    }
    //查看计划全部岗位信息
    @GetMapping("/topostsign/{planid}")
    public String toPostsign(@PathVariable("planid") String planid,Model model){
        List<Post> posts =postService.findbyplanid(planid);
        model.addAttribute("posts",posts);
        List<Recruitplan> list= recruitplanService.findAllRecruitplan(planid);
        model.addAttribute("planlist",list);

        return "plan/plan_tosignup";


    }

    //报名
    @RequestMapping("/signupadd")
    public String SignAdd(Signup signup,Model model){

        //通过计划编号查找到一个计划对象
        Recruitplan recruitplan =recruitplanService.findOneByplanid(signup.getPlanid());
        //通过学号查找到一个报名表对象
        Signup signup1 =signupService.selectBystuid(signup.getStuid());


        //判断学号是否已报名
        if (signup1!=null){
            model.addAttribute("msg","不能应聘两个岗位");
            System.out.println("不能应聘两次");
            return toSign(signup.getPostid(),model);
        }
        //判断计划人数是否已满
        if (recruitplan.getPersonnum()<=0){
            model.addAttribute("msg","该计划招聘人数已满");
            System.out.println("该计划招聘人数已满");
            return toSign(signup.getPostid(),model);
        }
        //报名成功计划人数-1
        Integer a =recruitplan.getPersonnum()-1;
        recruitplan.setPersonnum(a);
        recruitplanService.CheckRecruitplan(recruitplan);
        //报名成功状态默认为0
        signup.setResult("0");
        signup.setStatus("0");
        System.out.println(signup.getApplicant());
        System.out.println(signup.getStatus());
        signupService.AddSignup(signup);

        return toPostsign(signup.getPlanid(),model);

    }

    @RequestMapping("/Archivsesadd")
    public String addArchivses(Archivses archivses,String person,Model model){
        archivsesService.addArchivses(archivses);


        return AllSignup(person,model);
    }

    //保存面试成绩
    @RequestMapping("/interviewsave")
    public String savainterview(String labname,Interview interview,Model model,String person){
        //如果审核不通过跳转全部报名信息
        if (interview.getResult().equals("2")){
            interviewService.addinterview(interview);
            return AllSignup(person,model);
        }
        //审核通过跳转档案添加信息
        interviewService.addinterview(interview);
        //通过学号查看学生注册时的信息
        System.out.println(interview.getStuid());
        Register  register = registerService.findone(interview.getStuid());
        System.out.println(register);
        model.addAttribute("register",register);
        //查找全部实验室信息
        Collection<Laboratory> laboratory =laboratoryService.getAlllab(labname);
        model.addAttribute("laboratory",laboratory);

        return "archivses/archivses_add";
    }
    //面试通过将学生的身份变为助理
    @RequestMapping("/change/{stuid}")
    public String change(@PathVariable("stuid")String stuid){
        User user =userService.findoneUser(stuid);
        user.setRolename("实验室助理");
        userService.edituser(user);
        return "redirect:http://localhost:8080";
    }



}
